package co.yixiang.yshop.module.order.mq.consumer;

import co.yixiang.yshop.framework.mq.redis.core.stream.AbstractRedisStreamMessageListener;
import co.yixiang.yshop.module.order.service.storeorder.AppStoreOrderService;
import co.yixiang.yshop.module.pay.mq.message.PayNoticeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;

/**
 * 消息队列处理支付消息
 */
@Component
@Slf4j
public class PayNoticeConsumer extends AbstractRedisStreamMessageListener<PayNoticeMessage> {

    @Resource
    private AppStoreOrderService appStoreOrderService;

    @Override
    public void onMessage(PayNoticeMessage message) {
        log.info("==================== 收到支付通知MQ消息 ====================");
        log.info("[onMessage][支付消息内容] orderId={}, payType={}", message.getOrderId(), message.getPayType());
        
        try {
            appStoreOrderService.paySuccess(message.getOrderId(), message.getPayType());
            log.info("[onMessage][订单状态更新成功] orderId={}", message.getOrderId());
        } catch (Exception e) {
            log.error("[onMessage][订单状态更新失败] orderId={}, error={}", message.getOrderId(), e.getMessage(), e);
            throw e;  // 重新抛出异常，触发重试机制
        }
        
        log.info("==================== 支付通知处理完成 ====================");
    }
}
